﻿#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QThread>
#include <QPushButton>
#include <QLabel>
#include <QProgressBar>
#include <QLineEdit>
#include <QMutex>
#include <QRandomGenerator>

class WorkerThread : public QThread {
    Q_OBJECT

public:
    WorkerThread(QObject *parent = nullptr) : QThread(parent), shouldStop(false) {}
    void run() override;
    void requestStop();

signals:
    void progressUpdate(int percentage);      // 发送进度更新信号
    void resultReady(const QString &result);  // 任务完成时发送结果信号

private:
    bool shouldStop;
    QMutex mutex;
};

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

private slots:
    void startThread();
    void stopThread();
    void updateProgress(int percentage);
    void handleResults(const QString &result);

private:
    QPushButton *startButton;
    QPushButton *stopButton;
    QLabel *statusLabel;
    QProgressBar *progressBar;
    QLineEdit *resultField;
    WorkerThread *workerThread;
};
#endif // MAINWINDOW_H
